var cityArrr = localStorage.getItem("cityArr");
var cityArr = JSON.parse(cityArrr);
// 添加左侧城市
for (var i = 0; i < cityArr.length; i++) {
    $('.c_city select').append(
        `<option value="" data-id='${cityArr[i].cityId}'>${cityArr[i].cityName}</option>`
    )
}

// 点击左侧城市，添加右侧学校
$('option').click(addRightSchool);
$('.c_city select option:first').click();

function addRightSchool() {
    $('tbody tr').remove();
    var index = $(this).attr('data-id');

    var studentsStr = localStorage.getItem("schoolArr");
    var schoolArr = JSON.parse(studentsStr);
    var studentArrr = localStorage.getItem("studentArr");
    var studentArr = JSON.parse(studentArrr);

    for (var i = 0; i < schoolArr.length; i++) {
        var wmnum = [];
        var hynum = fhynum = 0;
        if (schoolArr[i].cityId == index) {
            $('tbody').append(
                `<tr>
                <td>${schoolArr[i].schoolName}</td>
            </tr>`
            )
            for (var j = 0; j < studentArr.length; j++) {
                if (schoolArr[i].id == studentArr[j].schoolId) {
                    if (wmnum.indexOf(studentArr[j].classes) == -1) {
                        wmnum.push(studentArr[j].classes);
                    }
                    if (studentArr[j].state == '会员') {
                        hynum++;
                    } else if (studentArr[j].state == '非会员') {
                        fhynum++;
                    }
                }
            }
            $('tbody tr:last').append(
                `<td>${wmnum.length}</td>
                <td>${hynum}</td>
                <td>${fhynum}</td>`
            )
        }
    }
}

// 搜索
$('.btn_sousuo').click(function() {
    var txt = $('.box_btn input').val();
    for (var i = 0; i < schoolArr.length; i++) {
        if (schoolArr[i].schoolName == txt) {
            var wmnum = [];
            var hynum = fhynum = 0;
            $('tbody tr').remove();
            $('tbody').append(
                `<tr>
                <td>${txt}</td>
            </tr>`
            )
            for (var j = 0; j < studentArr.length; j++) {
                if (schoolArr[i].id == studentArr[j].schoolId) {
                    if (wmnum.indexOf(studentArr[j].classes) == -1) {
                        wmnum.push(studentArr[j].classes);
                    }
                    if (studentArr[j].state == '会员') {
                        hynum++;
                    } else if (studentArr[j].state == '非会员') {
                        fhynum++;
                    }
                }
            }
            $('tbody tr').append(
                `<td>${wmnum.length}</td>
                <td>${hynum}</td>
                <td>${fhynum}</td>`
            )
            break;
        }
    }
})

/* for (var i = 0; i < cityArr.length; i++) {
    for (var j = 0; j < schoolArr.length; j++) {
        if (schoolArr[j].cityId == cityArr[i].cityId) {
            $('.tbody').append(
                `<tr>
                <td>${schoolArr[j].schoolName}</td>
            </tr>`
            )
        }
    }
} */